package com.quick.DP.ObserverPattern;

import java.util.ArrayList;
import java.util.List;

class WeatherStation implements WeatherSubject {
    private List<WeatherObserver> observers = new ArrayList<>();
    private float temperature;
    
    public void setTemperature(float temp) {
        this.temperature = temp;
        notifyObservers(); // 温度变化时自动通知所有观察者
    }
    
    @Override
    public void registerObserver(WeatherObserver o) {
        observers.add(o);
    }
    
    @Override
    public void removeObserver(WeatherObserver o) {
        observers.remove(o);
    }
    
    @Override
    public void notifyObservers() {
        for (WeatherObserver observer : observers) {
            observer.update(temperature);
        }
    }
}